require "Common/define"
require "Logic/Config"

UIHome = {};
HelperFunc.SetUIEnv(UIHome)
local this = UIHome;

this.LuaBehaviour = nil;
this.transform = nil;
this.gameObject = nil;

this.btn_chat = nil;
this.btn_func = nil;
this.ImgRole = nil;
this.ImgBannelFrame = nil;
this.ImgBannel1 = nil;
this.ImgBannel2 = nil;
this.ImgPointParent = nil;
this.ImgPoint = nil;
this.ImgPoint_S = nil;

this.hasInit = false;
this.curMouseXPos = nil;
this.curMouseYPos = nil;
this.chatPosX = nil;
this.chatPosY = nil;
this.Xoffset = 0;
this.Yoffset = 0;

local tab_top = {"activity1", "activity2", "activity3","activity1"};

function UIHome.Init(funid)
    local name = getUIName(PanelNames.UIHome);
    panelMgr:Push(name, this.onCreate,Config.useLocalUI);
end

--定义控件
function UIHome.onCreate(obj)
    gameObject = obj;
    transform = obj.transform;
    LuaBehaviour = transform:GetComponent('LuaBehaviour');

    if not hasInit then
        hasInit = true;

        --聊天按钮（临时）
        btn_chat = transform:Find("btn_Chat");
        --功能按钮
        btn_func = transform:Find("LeftTop/btn_Func");

        ImgRole = transform:Find("ImgRole"):GetComponent("Image");
        ImgBannelFrame = transform:Find("ImgBannelFrame");
        ImgBannel1 = transform:Find("ImgBannelFrame/ImgBannel1"):GetComponent("Image");
        ImgBannel2 = transform:Find("ImgBannelFrame/ImgBannel2"):GetComponent("Image");
        ImgBannel3 = transform:Find("ImgBannelFrame/ImgBannel3"):GetComponent("Image");
        ImgPointParent = transform:Find("ImgBannelFrame/ImgPointParent");
        ImgPoint = transform:Find("ImgBannelFrame/ImgPoint");
        ImgPoint_S = transform:Find("ImgBannelFrame/ImgPoint_S");

        if heroConfig == nil then
            heroConfig = G.dataTable["t_hero"]
        end
        local heroConfig = G.dataTable["t_hero"][tostring(logicMgr.HeroManager.GetRoleInfo().leaderModelId)]
        if heroConfig ~= nil then
            local imageName = heroConfig.f_HeroDrawing;
            resMgr:LoadSpritePrefab(imageName,function(sp)
                ImgRole.sprite = sp; 
            end)
        end

        this.addEvent();
    end

    local chatPosX = Util.GetPlayerPrefs("saveChatBtnPosX");
    local chatPosY = Util.GetPlayerPrefs("saveChatBtnPosY");
    if chatPosX ~= nil and chatPosY ~= nil then
        Util.Lua_SetLocalPositionXYZ(btn_chat.gameObject,chatPosX,chatPosY,0)
    end

    this.initUpdateBanner();
    this.star();
end

function UIHome.addEvent()
    LuaBehaviour:AddLongClick(btn_chat.gameObject, this.onLongClickChat);
    LuaBehaviour:AddClick(btn_func.gameObject, this.onClickFunc);
    LuaBehaviour:AddLongClick(ImgBannel1.gameObject, this.onLongClickBanner);
    LuaBehaviour:AddLongClick(ImgBannel2.gameObject, this.onLongClickBanner);
    LuaBehaviour:AddLongClick(ImgBannel3.gameObject, this.onLongClickBanner);
end

function UIHome.removeEvent()
    if LuaBehaviour ~= nil then
        LuaBehaviour:RemoveLongClick(btn_chat.gameObject);
        LuaBehaviour:RemoveClick(btn_func.gameObject);
        LuaBehaviour:RemoveClick(ImgBannel1.gameObject);
        LuaBehaviour:RemoveClick(ImgBannel2.gameObject);
        LuaBehaviour:RemoveClick(ImgBannel3.gameObject);
    end
end

function UIHome.LocalListenEvent()
end

function UIHome.LocalCloseEvent()
end

function UIHome.OnDestroy()
    this.removeEvent();
end

function UIHome.OnEnable()
    this.LocalListenEvent();
end

function UIHome.OnDisable()
    this.LocalCloseEvent();
    UpdateBeat:Remove(this.update, self);
    UpdateBeat:Remove(this.updateBanner, self);
end

function UIHome.onLongClickChat()
    inLongClick = true;
    curMouseXPos = Input.mousePosition.x;
    curMouseYPos = Input.mousePosition.y;
    local chatPosX,chatPosY,z = Util.Lua_GetLocalPositionXYZ(btn_chat.gameObject,0,0,0);
    UpdateBeat:Add(this.update, self);
end

function UIHome.update()
    if Input.GetMouseButton(0) then
        local mouseXPos = Input.mousePosition.x;
        local mouseYPos = Input.mousePosition.y;
        Xoffset = mouseXPos - curMouseXPos;
        Yoffset = mouseYPos - curMouseYPos;

        local limitX = Mathf.Clamp(chatPosX + Xoffset,-350,350);
        local limitY = Mathf.Clamp(chatPosY + Yoffset,-530,550);
        Util.Lua_SetLocalPositionXYZ(btn_chat.gameObject,limitX,limitY,0)
    end

    if Input.GetMouseButtonUp(0) then
        local x,y = Util.Lua_GetLocalPositionXYZ(btn_chat.gameObject,0,0,0);
        Util.SetPlayerPrefs("saveChatBtnPosX",x);
        Util.SetPlayerPrefs("saveChatBtnPosY",y);

        if Xoffset == 0 and Yoffset == 0 then
            showUI(PanelNames.UIChatPanel);
        end
        UpdateBeat:Remove(this.update, self);
    end
end

function UIHome.onClickFunc()
    showUI(PanelNames.UIFuncMenu);
end

local fightTimer = nil;
function UIHome.star()
	if fightTimer == nil then
        fightTimer = Timer.New(this.onUpdateBanner,5,999999,false);
    else
        fightTimer:Reset(this.onUpdateBanner,5,999999,false);
	end
	fightTimer:Start();
end

function UIHome.stop()
	if fightTimer ~= nil then
        fightTimer:Stop();
	end
end

function UIHome.initUpdateBanner()
    curPage = 1;
    nextPage = curPage + 1;
    nextPage = curPage + 1;
    if nextPage > #tab_top then
        nextPage = 1;
    end 
    prePage = curPage - 1;
    if prePage < 1 then
        prePage = #tab_top;
    end 

    resMgr:LoadSpritePrefab(tab_top[prePage],function(sp)
        ImgBannel1.sprite = sp;
    end);    
    resMgr:LoadSpritePrefab(tab_top[curPage],function(sp)
        ImgBannel2.sprite = sp;
    end);
    resMgr:LoadSpritePrefab(tab_top[nextPage],function(sp)
        ImgBannel3.sprite = sp;
    end);
    Util.Lua_SetLocalPositionXYZ(ImgBannel1.transform,-300,0,0);
    Util.Lua_SetLocalPositionXYZ(ImgBannel2.transform,0,0,0);
    Util.Lua_SetLocalPositionXYZ(ImgBannel2.transform,300,0,0);

    clearChild(ImgPointParent.transform,true);
    tab_PointItem = {}
    tab_PointItem2 = {}
    for i=1,#tab_top do
        local point = newObject(ImgPoint);
        point.transform:SetParent(ImgPointParent.transform);
        Util.Lua_SetLocalPositionXYZ(point.transform,(i-#tab_top/2)*20,0,0)
        Util.Lua_SetLocalScaleXYZ(point.transform,0,0,0);
        table.insert(tab_PointItem,point);
        local point2 = newObject(ImgPoint_S);
        point2.transform:SetParent(ImgPointParent.transform);
        Util.Lua_SetLocalPositionXYZ(point2.transform,(i-#tab_top/2)*20,0,0)
        Util.Lua_SetLocalScaleXYZ(point2.transform,0,0,0);
        table.insert(tab_PointItem2,point2);
        if curPage == i then
            point.gameObject:SetActive(false);
            point2.gameObject:SetActive(true);
        else
            point.gameObject:SetActive(true);
            point2.gameObject:SetActive(false);
        end
    end    
end

function UIHome.onUpdateBanner()
    ImgBannel1.transform:DOLocalMoveX(-600,1,false);
    ImgBannel2.transform:DOLocalMoveX(-300,1,false);
    ImgBannel3.transform:DOLocalMoveX(0,1,false):OnComplete(function()
        curPage = curPage + 1;
        if curPage > #tab_top then
            curPage = 1;
        end
        nextPage = curPage + 1;
        if nextPage > #tab_top then
            nextPage = 1;
        end 
        prePage = curPage - 1;
        if prePage < 1 then
            prePage = #tab_top;
        end 

        resMgr:LoadSpritePrefab(tab_top[prePage],function(sp)
            ImgBannel1.sprite = sp;
        end);    
        resMgr:LoadSpritePrefab(tab_top[curPage],function(sp)
            ImgBannel2.sprite = sp;
        end);
        resMgr:LoadSpritePrefab(tab_top[nextPage],function(sp)
            ImgBannel3.sprite = sp;
        end);
        Util.Lua_SetLocalPositionXYZ(ImgBannel1.transform,-300,0,0);
        Util.Lua_SetLocalPositionXYZ(ImgBannel2.transform,0,0,0);
        Util.Lua_SetLocalPositionXYZ(ImgBannel3.transform,300,0,0);
        for i=1,#tab_top do
            local point1 = tab_PointItem[i];
            local point2 = tab_PointItem2[i];
            if curPage == i then
                point1.gameObject:SetActive(false);
                point2.gameObject:SetActive(true);
            else
                point1.gameObject:SetActive(true);
                point2.gameObject:SetActive(false);
            end
        end    
    end);
end

function UIHome.onLongClickBanner()
    inLongClick = true;
    this.stop();
    curMouseXPos = Input.mousePosition.x;
    curMouseYPos = Input.mousePosition.y;
    local chatPosX,chatPosY = Util.Lua_GetLocalPositionXYZ(ImgBannel2.transform,0,0,0);
    UpdateBeat:Add(this.updateBanner, self);
end

function UIHome.updateBanner()
    if Input.GetMouseButton(0) then
        local mouseXPos = Input.mousePosition.x;
        Xoffset = mouseXPos - curMouseXPos;
        if chatPosX + Xoffset > 0 and chatPosX + Xoffset < 300 then
            Util.Lua_SetLocalPositionXYZ(ImgBannel1.transform,-300 + chatPosX + Xoffset,chatPosY,0);
            Util.Lua_SetLocalPositionXYZ(ImgBannel2.transform,chatPosX + Xoffset,chatPosY,0);
            Util.Lua_SetLocalPositionXYZ(ImgBannel3.transform,300 + chatPosX + Xoffset,chatPosY,0);
        elseif chatPosX + Xoffset < 0 and chatPosX + Xoffset > -300 then
            Util.Lua_SetLocalPositionXYZ(ImgBannel1.transform,-300 + chatPosX + Xoffset,chatPosY,0);
            Util.Lua_SetLocalPositionXYZ(ImgBannel2.transform,chatPosX + Xoffset,chatPosY,0);
            Util.Lua_SetLocalPositionXYZ(ImgBannel3.transform,300 + chatPosX + Xoffset,chatPosY,0);
        end
    end

    if Input.GetMouseButtonUp(0) then
        if Xoffset == 0 and Yoffset == 0 then
            showUI(PanelNames.UIChatPanel);
        end

        if Xoffset > 10 then
            ImgBannel1.transform:DOLocalMoveX(0,0.5,false);
            ImgBannel2.transform:DOLocalMoveX(300,0.5,false);
            ImgBannel3.transform:DOLocalMoveX(600,0.5,false):OnComplete(function()
                curPage = curPage - 1;
                if curPage < 1 then
                    curPage = #tab_top;
                end
                nextPage = curPage + 1;
                if nextPage > #tab_top then
                    nextPage = 1;
                end
                prePage = curPage - 1;
                if prePage < 1 then
                    prePage = #tab_top;
                end 
        
                resMgr:LoadSpritePrefab(tab_top[prePage],function(sp)
                    ImgBannel1.sprite = sp;
                end);    
                resMgr:LoadSpritePrefab(tab_top[curPage],function(sp)
                    ImgBannel2.sprite = sp;
                end);
                resMgr:LoadSpritePrefab(tab_top[nextPage],function(sp)
                    ImgBannel3.sprite = sp;
                end);
                Util.Lua_SetLocalPositionXYZ(ImgBannel1.transform,-300,0,0);
                Util.Lua_SetLocalPositionXYZ(ImgBannel2.transform,0,0,0);
                Util.Lua_SetLocalPositionXYZ(ImgBannel3.transform,300,0,0);
                for i=1,#tab_top do
                    local point1 = tab_PointItem[i];
                    local point2 = tab_PointItem2[i];
                    if curPage == i then
                        point1.gameObject:SetActive(false);
                        point2.gameObject:SetActive(true);
                    else
                        point1.gameObject:SetActive(true);
                        point2.gameObject:SetActive(false);
                    end
                end 

                this.star();
            end);
        elseif Xoffset < -10 then
            ImgBannel1.transform:DOLocalMoveX(-600,0.5,false);
            ImgBannel2.transform:DOLocalMoveX(-300,0.5,false);
            ImgBannel3.transform:DOLocalMoveX(0,0.5,false):OnComplete(function()
                curPage = curPage + 1;
                if curPage > #tab_top then
                    curPage = 1;
                end
                nextPage = curPage + 1;
                if nextPage > #tab_top then
                    nextPage = 1;
                end
                prePage = curPage - 1;
                if prePage < 1 then
                    prePage = #tab_top;
                end 
        
                resMgr:LoadSpritePrefab(tab_top[prePage],function(sp)
                    ImgBannel1.sprite = sp;
                end);    
                resMgr:LoadSpritePrefab(tab_top[curPage],function(sp)
                    ImgBannel2.sprite = sp;
                end);
                resMgr:LoadSpritePrefab(tab_top[nextPage],function(sp)
                    ImgBannel3.sprite = sp;
                end);
                Util.Lua_SetLocalPositionXYZ(ImgBannel1.transform,-300,0,0);
                Util.Lua_SetLocalPositionXYZ(ImgBannel2.transform,0,0,0);
                Util.Lua_SetLocalPositionXYZ(ImgBannel3.transform,300,0,0);
                for i=1,#tab_top do
                    local point1 = tab_PointItem[i];
                    local point2 = tab_PointItem2[i];
                    if curPage == i then
                        point1.gameObject:SetActive(false);
                        point2.gameObject:SetActive(true);
                    else
                        point1.gameObject:SetActive(true);
                        point2.gameObject:SetActive(false);
                    end
                end 

                this.star();
            end);
        end
        UpdateBeat:Remove(this.updateBanner, self);
    end
end

return UIHome;